<template>
  <div class="login">
    <div class="login-form f-c a-c j-c">
      <div class="login-title">生鲜商城管理平台</div>
      <div class="login-form-box f-c a-c j-c">
        <div class="name">登录</div>
        <el-form ref="loginFormRef" label-position="top" size="large" :model="loginForm" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input clearable v-model="loginForm.username">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input clearable v-model="loginForm.password" :type="isHide ? 'password' :'text'">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
              <template #suffix>
                <el-icon @click="isHide = !isHide">
                  <View v-if="isHide"/>
                  <Hide v-else/>
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="验证码" style="margin-bottom: 0" prop="code">
            <div class="f-r a-c" style="width: 100%">
              <div class="f-g">
                <el-input clearable v-model="loginForm.code">
                  <template #prefix>
                    <el-icon><Lock /></el-icon>
                  </template>
                </el-input>
              </div>
              <Identify style='display:inline-block;margin-left: 40px; height:40px;vertical-align:top;cursor:pointer;'
                        :identifyCode="code" :contentHeight='40' />
            </div>
          </el-form-item>
          <el-form-item>
            <div style="width: 100%;text-align: right" @click="handleRefreshCode" >看不清？点击刷新</div>
          </el-form-item>
        </el-form>
        <el-button size="large" style="width: 100%" type="primary" @click="login">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const router = useRouter();
const store = useStore();
import {User, Lock,View,Hide } from '@element-plus/icons-vue'
const checkCode = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入验证码'))
  }
  if (value.toLowerCase() !== code.value.toLowerCase()) {
    return callback(new Error('验证码错误'))
  }
  callback()
}
const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})
const loginForm = ref({
  username: 'admin',
  password: '123456',
  code: ''
})
const code = ref('')
const isHide = ref(true)
function handleRefreshCode() {
  code.value = (Math.random() * 8999 + 1000).toFixed(0).toString();
}
onMounted(() => {
  handleRefreshCode()
})
const loginFormRef = ref(null)
function login() {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log('submit!')
      await store.dispatch('login', loginForm.value)
      await router.replace('/')
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-image: url("~@/assets/login_bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  padding-top: 148px;
  overflow: auto;
}
.login-form {
  .login-title {
    height: 28px;
    margin-bottom: 62px;
    color: #FFFFFF;
    font-size: 28px;
  }
  .login-form-box{
    width: 513px;
    /* 矩形 217 */
    border-radius: 8px;
    background: #FFFFFF;
    padding: 29px 38px;
    .name  {
      font-size: 24px;
      color: #333333;
      margin-bottom: 27px;
    }
    .el-form {
      width: 100%;
    }
  }
}
</style>
